<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\api\logic;

use app\api\error\CodeBase;
use app\api\error\Common as CommonError;
use app\common\logic\User as CommonUser;
use \Firebase\JWT\JWT;

/**
 * 接口基础逻辑
 */
class Common extends ApiBase
{
    public static $commonUserLogic = null;
    
    /**
     * 基类初始化
     */
    public function __construct()
    {
        // 执行父类构造方法
        parent::__construct();
        
        empty(static::$commonUserLogic) && static::$commonUserLogic = new CommonUser();
    }
    /**
     * 友情链接
     */
    public function getBlogrollList()
    {
        
        return $this->modelBlogroll->getList([DATA_STATUS_NAME => DATA_NORMAL], true, 'sort desc,id asc', false);
    }

    /**
     * 发送验证码
     */
    public function sendVerificationCode($data = [])
    {
        // 验证参数
        $this->validateCommon->scene('send_code')->check($data);
        
        // 调用common层业务逻辑
        return static::$commonUserLogic->sendVerificationCode($data);
    }

    /**
     * 管理员JWT签发
     * @param array $member 管理员信息
     * @param int $expire 过期时间（秒）
     * @return array
     * @author 王勇程 <laughskydragon@qq.com>
     */
    public static function tokenSign($member = [], $expire = 2592000)
    {
        if (empty($member) || !isset($member['id'])) {
            throw new \InvalidArgumentException('管理员信息不能为空且必须包含id');
        }

        $payload = [
            'iss' => 'OneBase',
            'aud' => 'member',
            'iat' => time(),
            'exp' => time() + $expire,
            'data' => array_merge($member, ['member_id' => $member['id']])
        ];

        $token = JWT::encode($payload, API_KEY . JWT_KEY, 'HS256');
        
        return ['member_token' => $token];
    }

    /**
     * 普通用户JWT签发
     * @param array $user 用户信息
     * @param int $expire 过期时间（秒）
     * @return array
     * @author 王勇程 <laughskydragon@qq.com>
     */
    public static function userTokenSign($user = [], $expire = 2592000)
    {
        if (empty($user) || !isset($user['id'])) {
            throw new \InvalidArgumentException('用户信息不能为空且必须包含id');
        }

        $payload = [
            'iss' => 'OneBase',
            'aud' => 'user',
            'iat' => time(),
            'exp' => time() + $expire,
            'data' => array_merge($user, ['user_id' => $user['id']])
        ];

        $token = JWT::encode($payload, API_KEY . JWT_KEY, 'HS256');
        
        return ['user_token' => $token];
    }
}
